<%@ page contentType="text/html; charset=UTF-8" %>
<%!
/**
 * 判断是否为Ajax请求
 * @param request   HttpServletRequest
 * @return  是true, 否false
 */
public boolean isAjaxRequest(HttpServletRequest request) {
    String requestType = request.getHeader("X-Requested-With");
    if (requestType != null && requestType.equals("XMLHttpRequest")) {
        return true;
    } else {
        return false;
    }
}

public String sendRedirect(HttpServletRequest request,String goUrl){
	return "<script>window.location.href = '"+goUrl+"';</script>";
}

public String getAlertMessage(HttpServletRequest request,String txt){
	String msg = "alert('"+txt+"');";
	if(isAjaxRequest(request)){
		return msg;
	}else{
		return "<script>"+msg+"</script>";
	}
}

public String getAlertMessageAndBack(HttpServletRequest request,String txt){
	String msg = "alert('"+txt+"');";
	if(isAjaxRequest(request)){
		return msg;
	}else{
		return "<script>"+msg+";history.go(-1);</script>";
	}
}

public String getAlertMessageAndClose(HttpServletRequest request,String txt){
	String msg = "alert('"+txt+"');";
	if(isAjaxRequest(request)){
		return msg;
	}else{
		return "<script>"+msg+";window.close();</script>";
	}
}

public String getAlertMessageAndReload(HttpServletRequest request,String txt){
	String msg = "alert('"+txt+"');";
	if(isAjaxRequest(request)){
		return msg+"location.reload();";
	}else{
		return "<script>"+msg+"</script>";
	}
}

public String getAlertMessage(HttpServletRequest request,String txt,String goUrl){
	String msg = "alert('"+txt+"');";
	if(isAjaxRequest(request)){
		return msg;
	}else{
		return "<script>"+msg+"window.location.href = '"+goUrl+"';</script>";
	}
}

%>